<template>
    <el-breadcrumb separator-icon="ArrowRight" style="margin-bottom: 50px;">
    <el-breadcrumb-item :to="{ path: '/home' }">主页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/home/projectList' }">项目列表</el-breadcrumb-item>
    <el-breadcrumb-item>添加项目</el-breadcrumb-item>
  </el-breadcrumb>
    <el-form ref="addRuleForm" label-width="100px" :model="field" :rules="form_rules">
        <el-form-item label="上传项目：">
            <el-form-item>
                <el-upload class="upload-demo" drag action="#" :show-file-list="true" :auto-upload="false"
                    :http-request="handlerUpload" :limit="1" accept=".zip">
                    <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                    <div class="el-upload__text">
                        将文件拖拽到此处 <em>点击上传</em>
                    </div>
                    <template #tip>
                        <div class="el-upload__tip">
                            只能上传一个文件（后缀为.zip）
                        </div>
                    </template>
                </el-upload>
            </el-form-item>
        </el-form-item>
        <el-form-item label="项目名称：" prop="name" >
            <el-input v-model="field.name" placeholder="请输入项目名称"></el-input>
        </el-form-item>
        <el-form-item label="项目版本：" prop="version" >
            <el-input v-model="field.version" placeholder="请输入项目版本"></el-input>
        </el-form-item>
        <el-form-item label="项目详情：" prop="description">
            <el-input v-model="field.description" type="textarea" placeholder="请输入项目详情"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="success" @click="submitForm('addRuleForm')">提交</el-button>
            <el-button type="danger" @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>s
</template>

<script>
import { reactive, ref, getCurrentInstance, unref, onMounted, nextTick } from 'vue';
import { UploadFilled } from '@element-plus/icons-vue'
export default {
    name: "ProjectAdd",
    setup(props) {

        //form表单
        /*
        const form_data = reactive({
            field: {
                name: "",
                version: "",
                description: "",
            },
            form_rules: {
                name: [{ required: true, message: "项目名称不能为空", trigger: 'blur' }],
                version: [{ required: true, message: "项目版本不能为空", trigger: 'blur' }],
                description: [{ required: true, message: "项目详情不能为空", trigger: 'blur' }],

            }

        })
        */
        const field = reactive({
                name: "",
                version: "",
                description: "",
        })
        const form_rules = reactive({
                name: [{ required: true, message: "项目名称不能为空", trigger: 'blur' }],
                version: [{ required: true, message: "项目版本不能为空", trigger: 'blur' }],
                description: [{ required: true, message: "项目详情不能为空", trigger: 'blur' }],

        })
        const { proxy } = getCurrentInstance()
        console.log(proxy)

        //const addRuleForm = ref(null);
        //console.log(addRuleForm)

        const submitForm = () => {
            proxy.$refs.addRuleForm.validate( (valid) => {
                console.log(valid)
                if (valid) {
                    
                    console.log('success submit!!')
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        }

        //定义可重用函数和初始数据
        const originData = () => ({
            name: "",
            version: "",
            description: "",
        });

        // 重置
        const resetForm = () => {
           // addRuleForm.value = { ...field }
           // Object.assign(form_data.field, originData());
            //addRuleForm.value.resetFields();
            proxy.$refs.addRuleForm.resetFields();
        }

         onMounted(()=>{
             Object.assign(field, originData());
         })

        return {
           // form_data,
            field,
            form_rules,
           // ...toRefs(form_data),
            submitForm,
            resetForm,
        }
    }
}
</script>

<style scoped></style>  